package com.yuyou.tas.lesson.service.impl;


import com.yuyou.tas.lesson.mapper.BaseSectionSubjectRelMapper;
import com.yuyou.tas.lesson.service.IBaseSSRService;
import com.yuyou.tas.service.api.lesson.dto.DtoBaseSSR;
import com.yuyou.tas.service.api.lesson.entity.BaseSectionSubjectRel;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.tool.Sequence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BaseSSRServiceImpl implements IBaseSSRService {

    @Autowired
    private BaseSectionSubjectRelMapper mapper;


    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public ResBoolSimpleInfo addSSR(Long subjectID, String[] gradeIds, Integer isTan, LoginRedisObj user,Long organId) {
        ResBoolSimpleInfo info = new ResBoolSimpleInfo(true,"插入成功");
        BaseSectionSubjectRel bssr = new BaseSectionSubjectRel();
        bssr.setId(Sequence.getSequence().nextId());
        bssr.setBaseSectionId(String.join(",",gradeIds));
        bssr.setAuthOrganizationId(organId);
        bssr.setCreateTime(new Date());
        bssr.setDataSubject(subjectID);
        bssr.setIsStan(isTan);
        mapper.insert(bssr);
        return info;
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public ResBoolSimpleInfo updateSSR(Long subjectID, String[] gradeIds, Integer isTan, Long id) {
        ResBoolSimpleInfo info = new ResBoolSimpleInfo(true,"更新成功");
        BaseSectionSubjectRel bssr = new BaseSectionSubjectRel();
        bssr.setId(id);

        if(gradeIds != null){
            bssr.setBaseSectionId(String.join(",",gradeIds));
        }

        bssr.setIsStan(isTan);
        bssr.setDataSubject(subjectID);
        mapper.updateByPrimaryKeySelective(bssr);

        return info;
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public ResBoolSimpleInfo delSSR(Long id) {
        ResBoolSimpleInfo info = new ResBoolSimpleInfo(true,"删除成功");
        mapper.deleteByPrimaryKey(id);
        return info;
    }

    @Override
    public ResBoolSimpleInfo<BaseSectionSubjectRel> getSSRByID(Long id) {
        BaseSectionSubjectRel ssr = mapper.selectByPrimaryKey(id);
        ResBoolSimpleInfo info = new ResBoolSimpleInfo(true,null,ssr);
        return info;
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public int findSSRCount(Pagination<DtoBaseSSR> queryPgn) {
        return mapper.findSSRCount(queryPgn);
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public List<DtoBaseSSR> findSSRPageList(Pagination<DtoBaseSSR> queryPgn) {
        return mapper.findSSRPageList(queryPgn);
    }

    @Override
    public BaseSectionSubjectRel selectByPKandOrgan(Long id, Long authOrganizationId) {
        return mapper.selectByPKandOrgan(id, authOrganizationId);
    }
}